/*

    This file is part of Hometree.

    Hometree is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Hometree is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Hometree.  If not, see <http://www.gnu.org/licenses/>.

    Copyright 2011,2012 Roman Held

*/

/********************************************************************\
                            Includes
\********************************************************************/
#include <QObject>
#include <QDebug>
#include <QPluginLoader>
#include <QCoreApplication>
#include <QStringList>
#include <QFile>
#include "htrsplugins.h"
#include "htr/htrpersistentservice.h"
#include "htr/htrlog.h"
#include "htrsmimedataresource.h"

/********************************************************************\
                          defines
\********************************************************************/
#define PS_SERVICE_ARG "--psplugin="

/********************************************************************\
                          Declaration
\********************************************************************/
HtrsPlugins *HtrsPlugins::mInstance = NULL;

/*-------------------------------------------------------------------
*/
HtrsPlugins *HtrsPlugins::instance()
{
  if (!mInstance)
    mInstance = new HtrsPlugins();
  return mInstance;
}

/*-------------------------------------------------------------------
*/
HtrPersistentService *HtrsPlugins::persistentService()
{
  return mPersistentService;
}

/*-------------------------------------------------------------------
*/
HtrsPlugins::HtrsPlugins()
{
  mPersistentService = NULL;
  init();
}

/*-------------------------------------------------------------------
*/
HtrsPlugins::~HtrsPlugins()
{
}

/*-------------------------------------------------------------------
*/
void HtrsPlugins::init()
{
  QString psPlugin = "../plugins/libplgservicedummy.so"; // TODO: ...?

  // Test program arguments:
  foreach(QString nextarg, qApp->arguments()) {
    if (nextarg.contains(PS_SERVICE_ARG)) {
      QStringList parts = nextarg.split("=");
      if ((parts.count()) == 2 && (QFile::exists(parts[1])))
        psPlugin = parts[1];
    }
  }

  HtrLog::instance()->info("HtrsPlugins",tr("persistent service: try file %1").arg(psPlugin));
  QPluginLoader psPluginLoader(psPlugin);
  mPersistentService = dynamic_cast<HtrPersistentService*>(psPluginLoader.instance());
  HtrLog::instance()->info("HtrsPlugins",tr("persistent service: %1").arg(mPersistentService ? mPersistentService->name() : QString("<not found>")));

  if (mPersistentService)
    initPersistentServiceFactories();
}

/*-------------------------------------------------------------------
*/
void HtrsPlugins::initPersistentServiceFactories()
{
  Q_ASSERT(mPersistentService);
  mPersistentService->addFactory(HtrsMimeDataResource::staticMetaObject);
}

